Ext.define('js.user.UserGroupForm', {
    extend: 'Ext.form.Panel',
    requires: ['js.Util', 'js.ctrl.Common'],
    controller: 'Common',
    bodyPadding: 5,
    width: '50%',
    scrollable: true,
    height: '60%',
    defaults: {
        border: true,
        defaults: {margin: 0},
        padding: 0,
        bodyPadding: 1,
        xtype: 'panel',
        margin:'0 58% 0 29%',
    },
    layout: {
        type: 'vbox',
        pack: 'start',
        align: 'stretch'
    },
    url: 'publicUserManagementRoutes',
    initComponent: function () {
        let departmentId={
            xtype: 'combobox',
            height: 36,
            width:'25%',
            name:'department_id',
            emptyText: '部门名称',
            margin:'20 58% 10 29%',
            fieldLabel: '部门名称',
            triggerAction: 'all',
            editable: false,
            allowBlank: false,
            displayField: 'dataType',
            valueField: 'value',
            store:  Ext.create('js.store.inner.RoleManagentQueryOneStore'),
        }
        let groupId = Ext.create('Ext.form.field.Hidden', {
            name: 'group_id',
        });

        let groupName = Ext.create('Ext.form.field.Text', {
            name: 'group_name',
            allowBlank: false,
            fieldLabel: '用户组名称',
            margin:'20 58% 10 29%',
        });
        let sfcg ={
            xtype: 'radiogroup',
            fieldLabel: '是否超管',
            id:'sfcg',
            name:'sfcg',
            vertical: true,
            items: [
                { boxLabel: '是', inputValue: '1',margin:'2 0 0 0',id:'sfcgA'},
                { boxLabel: '否', inputValue: '0',margin:'2 0 0 -120',id:'sfcgB'}
            ],
            listeners :{
                render:function (value) {
                    let rec = Ext.getCmp('RoleManagementShowPage').getSelection().shift();
                    if (typeof(rec) == "undefined") {
                        Ext.getCmp('sfcgA').setValue(true);
                    }else{
                        var bizVal = rec.data.is_manager;
                        if (bizVal == 0) {
                            Ext.getCmp('sfcgB').setValue(true);
                        } else {
                            Ext.getCmp('sfcgA').setValue(true);
                        }
                    }
                }
            }

        };
        let cgps={
            xtype: 'box',
            margin:'0 58% 10 29%',
            html: '说明：选择<span class="access_menu"> 是 </span>代表拥有所有权限 ，选择<span class="access_menu"> 否 </span>请在下方勾选所需要的权限'
        }
        let ps = {
            xtype: 'box',
            html: '说明：以下<span class="access_menu">红色</span>字体代表“菜单”，<span class="access_button">绿色</span>字体代表“功能按钮”'
        };

       /* let rowOne = {
            layout: 'hbox',
            items: [departmentId,groupId, groupName]
        };
*/
        let checkedChange = function (field, newValue) {
            if (field.parent) {
                let parent = this.query(`checkbox[name=${field.parent}]`).shift();
                if (newValue) {
                    parent.setRawValue(true);
                }
            } else {
                let children = this.query(`checkbox[parent=${field.name}]`);
                Ext.Array.map(children, (child) => {
                    child.setValue(newValue);
                });
            }
        };

        let engineeringPicker = {
            items: [{
                xtype: 'checkboxfield',
                boxLabel: '<span class="access_menu">工程管理</span>', label: '工程管理',
                name: 'project.engineeringSupervisionPage', type: 'menu',
                listeners: {
                    change: checkedChange, scope: this
                }
            }, {
                xtype: 'checkboxfield',
                xtype: 'container', padding: '0 45', layout: 'hbox',
                defaultType: 'checkboxfield',
                defaults: {padding: '0 25 0 0'},
                items: [{
                    boxLabel: '<span class="access_button">添加</span>',
                    name: 'engineering-btn-add', type: 'button'
                }, {
                    boxLabel: '<span class="access_button">编辑</span>',
                    name: 'engineering-btn-edit', type: 'button'
                }, {
                    boxLabel: '<span class="access_button">删除</span>',
                    name: 'engineering-btn-delete', type: 'button'
                }
                ]
            }]
        };
        let schedulePicker = {
            items: [{
                xtype: 'checkboxfield',
                boxLabel: '<span class="access_menu">日常监督安排</span>', label: '日常监督安排',
                name: 'schedule.ScheduleListPage', type: 'menu',
                listeners: {
                    change: checkedChange, scope: this
                }
            },]
        };
        let ledgerPicker = {
            items: [{
                xtype: 'checkboxfield',
                boxLabel: '<span class="access_menu">节点验收台账</span>', label: '节点验收台账',
                name: 'ledger.LedgerListPage', type: 'menu',
                listeners: {
                    change: checkedChange, scope: this
                }
            },]
        };
        let reportPicker = {
            items: [{
                xtype: 'checkboxfield',
                boxLabel: '<span class="access_menu">统计报表</span>', label: '统计报表',
                name: 'report.StatisticalStatementMain', type: 'menu',
                listeners: {
                    change: checkedChange, scope: this
                }
            }/*, {
                boxLabel: '<span class="access_menu">报表1</span>', label: '报表1',
                padding: '0 25', parent: 'report',
                name: 'report.Report1', type: 'menu',
                listeners: {
                    change: checkedChange, scope: this
                }
            }, {
                xtype: 'container', padding: '0 45', layout: 'hbox',
                defaultType: 'checkboxfield',
                defaults: {padding: '0 25 0 0'},
                items: [{
                    boxLabel: '<span class="access_button">添加</span>',
                    name: 'report1-btn-add', type: 'button'
                }, {
                    boxLabel: '<span class="access_button">修改</span>',
                    name: 'report1-btn-edit', type: 'button'
                }, {
                    boxLabel: '<span class="access_button">删除</span>',
                    name: 'report1-btn-delete', type: 'button'
                }
                ]
            }, {
                boxLabel: '<span class="access_menu">报表2</span>', label: '报表2',
                padding: '0 25', parent: 'report',
                name: 'report.Report2', type: 'menu',
                listeners: {
                    change: checkedChange, scope: this
                }
            }, {
                xtype: 'container', padding: '0 45', layout: 'hbox',
                defaultType: 'checkboxfield',
                defaults: {padding: '0 25 0 0'},
                items: [{
                    boxLabel: '<span class="access_button">修改</span>',
                    name: 'report2-btn-edit', type: 'button'
                }, {
                    boxLabel: '<span class="access_button">删除</span>',
                    name: 'report2-btn-delete', type: 'button'
                }
                ]
            }*/]
        };

        let systemLayout = {
            items: [{
                xtype: 'checkboxfield',
                boxLabel: '<span class="access_menu">系统配置</span>', label: '系统配置',
                name: 'system', type: 'pz',
                listeners: {
                    change: checkedChange, scope: this
                }
            }, {
                xtype: 'checkboxfield',
                boxLabel: '<span class="access_menu">城乡区域</span>', label: '城乡区域',
                padding: '0 25', parent: 'system',
                name: 'system.RegionListPageMain', type: 'pz',
                listeners: {
                    change: checkedChange, scope: this
                }
            }, {
                xtype: 'checkboxfield',
                xtype: 'container', padding: '0 45', layout: 'hbox',
                defaultType: 'checkboxfield',
                defaults: {padding: '0 25 0 0'},
                items: [{
                    boxLabel: '<span class="access_button">添加</span>',
                    name: 'region-btn-add', type: 'button'
                }, {
                    boxLabel: '<span class="access_button">修改</span>',
                    name: 'region-btn-edit', type: 'button'
                }, {
                    boxLabel: '<span class="access_button">删除</span>',
                    name: 'region-btn-delete', type: 'button'
                }
                ]
            }, {
                xtype: 'checkboxfield',
                boxLabel: '<span class="access_menu">涉及单位</span>', label: '涉及单位',
                padding: '0 25', parent: 'system',
                name: 'system.UnitPageListPageMain', type: 'pz',
                listeners: {
                    change: checkedChange, scope: this
                }
            }, {
                xtype: 'checkboxfield',
                xtype: 'container', padding: '0 45', layout: 'hbox',
                defaultType: 'checkboxfield',
                defaults: {padding: '0 25 0 0'},
                items: [{
                    boxLabel: '<span class="access_button">添加</span>',
                    name: 'unit-btn-add', type: 'button'
                },{
                    boxLabel: '<span class="access_button">修改</span>',
                    name: 'unit-btn-edit', type: 'button'
                }, {
                    boxLabel: '<span class="access_button">删除</span>',
                    name: 'unit-btn-delete', type: 'button'
                }
                ]
            },{
                xtype: 'checkboxfield',
                boxLabel: '<span class="access_menu">工程结构类型</span>', label: '工程结构类型',
                padding: '0 25', parent: 'system',
                name: 'system.EngineeringStructureTypePageMain', type: 'pz',
                listeners: {
                    change: checkedChange, scope: this
                }
            }, {
                xtype: 'checkboxfield',
                xtype: 'container', padding: '0 45', layout: 'hbox',
                defaultType: 'checkboxfield',
                defaults: {padding: '0 25 0 0'},
                items: [{
                    boxLabel: '<span class="access_button">添加</span>',
                    name: 'cost-btn-add', type: 'button'
                }, {
                    boxLabel: '<span class="access_button">修改</span>',
                    name: 'cost-btn-edit', type: 'button'
                }, {
                    boxLabel: '<span class="access_button">删除</span>',
                    name: 'cost-btn-delete', type: 'button'
                }
                ]
            },{
                xtype: 'checkboxfield',
                boxLabel: '<span class="access_menu">建筑用途</span>', label: '建筑用途',
                padding: '0 25', parent: 'system',
                name: 'system.ConstructionApplicationsPageMain', type: 'pz',
                listeners: {
                    change: checkedChange, scope: this
                }
            }, {
                xtype: 'container', padding: '0 45', layout: 'hbox',
                defaultType: 'checkboxfield',
                defaults: {padding: '0 25 0 0'},
                items: [{
                    boxLabel: '<span class="access_button">添加</span>',
                    name: 'applications-btn-add', type: 'button'
                }, {
                    boxLabel: '<span class="access_button">修改</span>',
                    name: 'applications-btn-edit', type: 'button'
                }, {
                    boxLabel: '<span class="access_button">删除</span>',
                    name: 'applications-btn-delete', type: 'button'
                }
                ]
            }]
        };
        let innerManagement  = {
            items: [{
                xtype: 'checkboxfield',
                boxLabel: '<span class="access_menu">内部管理</span>', label: '内部管理',
                name: 'inner', type: 'menu',
                listeners: {
                    change: checkedChange, scope: this
                }
            }, {
                xtype: 'checkboxfield',
                boxLabel: '<span class="access_menu">部门管理</span>', label: '部门管理',
                padding: '0 25', parent: 'inner',
                name: 'inner.DivisionalManagement', type: 'menu',
                listeners: {
                    change: checkedChange, scope: this
                }
            }, {
                xtype: 'checkboxfield',
                boxLabel: '<span class="access_menu">角色管理</span>', label: '角色管理',
                padding: '0 25', parent: 'inner',
                name: 'inner.RoleManagementMain', type: 'menu',
                listeners: {
                    change: checkedChange, scope: this
                }
            }, {
                xtype: 'checkboxfield',
                boxLabel: '<span class="access_menu">人员管理</span>', label: '人员管理',
                padding: '0 25', parent: 'inner',
                name: 'inner.PeopleManagement', type: 'menu',
                listeners: {
                    change: checkedChange, scope: this
                }
            }]
        };

        this.buttons = [{
            text: '保存', handler: () => {
                this.submit();
            },
            width:140,
            height:42,
            cls:'btnSubmit',
            margin:'0 20 20 0',
            scope: this
        }];

        this.items = [departmentId,groupId, groupName,sfcg,cgps, ps,engineeringPicker,schedulePicker,ledgerPicker,reportPicker,innerManagement,systemLayout ];

        this.getValues = function (sfcg) {
            /*//如果是1超管不然就不是超管*/
            let menus;
            let buttons;
            if(sfcg==1){
                menus = this.query('[type=menu]');
                buttons = this.query('[type=button]');
            }else{
                menus = this.query('checkbox[checked][type=menu]');
                buttons = this.query('checkbox[checked][type=button]');
            }
            let parent = {}, menu_name = [];
            Ext.Array.each(menus, function (menu) {
                if (!menu.parent) {
                    parent = {id: menu.name, text: menu.label, iconCls: menu.iconCls, children: [], leaf: true};
                    menu_name.push(parent);
                } else {
                    parent.children.push({
                        id: menu.name, text: menu.label, iconCls: menu.iconCls, leaf: true
                    });
                    parent.leaf = false;
                }
            });
            let button_name = Ext.Array.map(buttons, (button) => {
                return button.getName();
            });

            return {
                group_id: groupId.getValue(),
                group_name: groupName.getValue(),
                sfcg: sfcg,
                sys_menu_id: Ext.encode(menu_name),
                sys_button_id: button_name.join(',')
            };
        };

        this.setValues = function (values) {
            let form = this.getForm();
            form.setValues(values);

            util.cascade(Ext.decode(values.sys_menu_id || '[]'), (menu) => {
                let field = form.findField(menu.id);
                if (field) field.setRawValue(true);
            });

            Ext.Array.each(values.sys_button_id.split(','), (name) => {
                let button = form.findField(name);
                if (button) button.setValue(true);
            });
        };

        this.submit = function () {
            //获取单选框的内容 是否是超管
         /*   let sfcg = Ext.getCmp('sfcg').getValue().sfcg?Ext.getCmp('sfcg').getValue():Ext.getCmp('sfcg').value;*/
            let sfcg =  Ext.getCmp('sfcg').lastValue
            let form = this.getForm();
            let params = this.getValues(sfcg.sfcg);
            params.action_type = groupId.getValue() ? 'userGroupUpdate' : 'userGroupAdd';
            form.submit({params: params, waitMsg: '正在保存...'});
        };

        this.on('actioncomplete', this.success, this)
            .on('actionfailed', this.fail, this);

        this.callParent(arguments);
    },
    success: function (response) {
        Ext.toast(`保存成功`);
        this.up('window').close();
        Ext.getCmp('RoleManagementShowPage').getStore().reload();
    },
    fail: function (form, action) {
        let msg = '';
        if (action.failureType === 'client') {
            msg = '请填写必填项';
        } else if (action.failureType === 'connect') {
            msg = '与服务器失去连接';
        } else if (action.failureType === 'server') {
            msg = action.result.message || '服务器错误';
        } else {
            msg = '未知错误';
        }
        Ext.toast(`保存失败，${msg}`);
    }
});